package dicapi

import (
	// "net/http"
	"encoding/json"
	"strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	suppliertable struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		SupplierName 				string `json:"suppliername" bson:"suppliername"`
		SupplierType				[]string `json:"suppliertype" bson:"suppliertype"`
		ForShort					string `json:"forshort" bson:"forshort"`
		Contact						string `json:"contact" bson:"contact"`
		MobilePhone					string `json:"mobilephone" bson:"mobilephone"`
		EmailAddress				string `json:"emailaddress" bson:"emailaddress"`
		ContactAddress				string `json:"contactaddress" bson:"contactaddress"`
		Developer					string `json:"developer" bson:"developer"`
		PayMethod					string `json:"paymethod" bson:"paymethod"`
		KnitWoven					string `json:"knitwoven" bson:"knitwoven"`
		GoodAtCategory				string `json:"goodatcategory" bson:"goodatcategory"`
		ProductionLinePerson		string `json:"productionlineperson" bson:"productionlineperson"`
		ProductionLineNumber		string `json:"productionlinenumber" bson:"productionlinenumber"`
		SingleLineDailyCapacity		string `json:"singlelinedailycapacity" bson:"singlelinedailycapacity"`
		PlanOrderQuantity			string `json:"planorderquantity" bson:"planorderquantity"`
		WhetherLaboratory			string `json:"whetherlaboratory" bson:"whetherlaboratory"`
		WhetherTeam					string `json:"whetherteam" bson:"whetherteam"`
		SingleColorOrderQuantity	string `json:"singlecolororderquantity" bson:"singlecolororderquantity"`
		SamplePromptDeliveryRate	string `json:"samplepromptdeliveryrate" bson:"samplepromptdeliveryrate"`
		TranspositionRate			string `json:"transpositionrate" bson:"transpositionrate"`
		DefectiveRate				string `json:"defectiverate" bson:"defectiverate"`
		PercentOfPassRate			string `json:"percentofpassrate" bson:"percentofpassrate"`
		MajorQualityAccident		string `json:"majorqualityaccident" bson:"majorqualityaccident"`
		MajorAccidentRecord			string `json:"majoraccidentrecord" bson:"majoraccidentrecord"`
		Note						string `json:"note" bson:"note"`
	}
	insertsuppliertable struct {
		SupplierName 				string `json:"suppliername" bson:"suppliername"`
		SupplierType				[]string `json:"suppliertype" bson:"suppliertype"`
		ForShort					string `json:"forshort" bson:"forshort"`
		Contact						string `json:"contact" bson:"contact"`
		MobilePhone					string `json:"mobilephone" bson:"mobilephone"`
		EmailAddress				string `json:"emailaddress" bson:"emailaddress"`
		ContactAddress				string `json:"contactaddress" bson:"contactaddress"`
		Developer					string `json:"developer" bson:"developer"`
		PayMethod					string `json:"paymethod" bson:"paymethod"`
		KnitWoven					string `json:"knitwoven" bson:"knitwoven"`
		GoodAtCategory				string `json:"goodatcategory" bson:"goodatcategory"`
		ProductionLinePerson		string `json:"productionlineperson" bson:"productionlineperson"`
		ProductionLineNumber		string `json:"productionlinenumber" bson:"productionlinenumber"`
		SingleLineDailyCapacity		string `json:"singlelinedailycapacity" bson:"singlelinedailycapacity"`
		PlanOrderQuantity			string `json:"planorderquantity" bson:"planorderquantity"`
		WhetherLaboratory			string `json:"whetherlaboratory" bson:"whetherlaboratory"`
		WhetherTeam					string `json:"whetherteam" bson:"whetherteam"`
		SingleColorOrderQuantity	string `json:"singlecolororderquantity" bson:"singlecolororderquantity"`
		SamplePromptDeliveryRate	string `json:"samplepromptdeliveryrate" bson:"samplepromptdeliveryrate"`
		TranspositionRate			string `json:"transpositionrate" bson:"transpositionrate"`
		DefectiveRate				string `json:"defectiverate" bson:"defectiverate"`
		PercentOfPassRate			string `json:"percentofpassrate" bson:"percentofpassrate"`
		MajorQualityAccident		string `json:"majorqualityaccident" bson:"majorqualityaccident"`
		MajorAccidentRecord			string `json:"majoraccidentrecord" bson:"majoraccidentrecord"`
		Note						string `json:"note" bson:"note"`
	}
)

func GetSupplierTableListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyDic"
	model.DB().Document="supplier_table"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]suppliertable{}
	query.All(&ps)
	
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})

	return nil
	
}
func AddSupplierTableHandler(c echo.Context) error {
	s:= &insertsuppliertable{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	model.DB().Dbname="OxyDic"
	model.DB().Document="supplier_table"
	err:=model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
	
}
func UpdateSupplierTableHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdateSupplierTableHandler",id)
	
	s:= &insertsuppliertable{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	
	model.DB().Dbname="OxyDic"
	model.DB().Document="supplier_table"
	err:= model.DB().Update( bson.M{"_id":bson.ObjectIdHex(id)} , s)
	if err != nil{
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func DelSupplierTableHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelSupplierTableHandler",id)
	model.DB().Dbname="OxyDic"
	model.DB().Document="supplier_table"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}